import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @author wcj
 * @Date 2021/6/8 11:46
 * @Version 1.0
 */
public class Piped {

    /**
     * 使用管道进行线程之间通信：
     * PipedInputStream && PipedOutputStream:面向字节
     * PipedReader && PipedWriter：面向字符
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        out.connect(in);

        Thread thread = new Thread(new Print(in), "Piped Print");
        thread.start();

        int receive = 0;
        try {
            while ((receive = System.in.read()) != -1) {
                out.write(receive);
            }
        } catch (IOException e) {
            System.out.println(e);
        }

    }

    static class Print implements Runnable {
        private PipedReader in;

        public Print(PipedReader in) {
            this.in = in;
        }

        @Override
        public void run() {
            try {
                int receive = 0;
                while ((receive = in.read()) != -1) {
                    System.out.println((char) receive);
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}
